import { login, logout } from '@/api/login/login'
import { updateInfo, getUtils } from '@/api/user/myInfo'
import { getPhoto } from "@/api/user/myAccount";
import { Message } from 'element-ui'
import { cloneDeep } from 'lodash'
export default {
    namespaced: true,
    state: {
        userInfo: null || JSON.parse(sessionStorage.getItem('userInfo')),
        nowRoot: '教师' || JSON.parse(sessionStorage.getItem('nowRoot')),
        rootArr: null || JSON.parse(sessionStorage.getItem('rootArr')),
        imgUrl: null || JSON.parse(sessionStorage.getItem('imgUrl')),
        utils: null || JSON.parse(sessionStorage.getItem('utils'))
    },

    mutations: {
        //    登录
        LOGIN_(state, data) {
            const { sciUser, rootName } = data
            // 用户个人信息
            state.userInfo = sciUser
                // 用户权限数组
            state.rootArr = rootName

        },
        // 退出登录
        LOGOUT_(state) {
            state.userInfo = null
            state.nowRoot = '教师'
            state.rootArr = null
            state.imgUrl = null
            state.utils = null

        },


        // 获取当前身份
        GETROOT_(state, data) {
            state.nowRoot = data
        },

        // 更新个人信息
        UPDATEINFO_(state, data) {
            state.userInfo = cloneDeep(data)
        },


        GETPHOTO_(state, url) {
            state.imgUrl = url
        },


        GETUTILS_(state, data) {
            state.utils = data
        }
    },

    actions: {
        // 登陆接口
        async login_({ commit }, { userId, userPassword }) {
            let result = await login(userId, userPassword)
            if (result.code === 1) {
                commit('LOGIN_', result.data)
                sessionStorage.setItem('userInfo', JSON.stringify(result.data.sciUser))
                sessionStorage.setItem('nowRoot', JSON.stringify(result.data.rootName[0]))
                sessionStorage.setItem('rootArr', JSON.stringify(result.data.rootName))

                Message({ type: 'success', message: '登录成功' })
            } else {
                Message({ type: 'error', message: result.msg })
                return false
            }
            return result

        },


        // 退出登录
        async logout_({ commit }) {
            let result = await logout()
            if (result.code === 1) {
                sessionStorage.clear()
                commit('LOGOUT_')
                Message({ type: 'success', message: '退出成功' })
                return true
            }
            Message({ type: 'error', message: result.msg })

        },

        // 切换身份
        changeRoot_({ commit }, root) {
            Message({ type: 'success', message: `当前身份为${root}` })
            sessionStorage.setItem('nowRoot', JSON.stringify(root))
            commit('GETROOT_', root)
            return true

        },

        // 更新个人信息
        async updateInfo_({ commit }, data) {
            let result = await updateInfo(data)
            if (result.code === 1) {
                commit('UPDATEINFO_', data)

            }
            return result
        },



        async getPhoto_({ commit }) {
            let result = await getPhoto()
            let blob = new Blob([result]); // 返回的文件流数据
            let url = window.URL.createObjectURL(blob); // 将他转化为路径
            commit('GETPHOTO_', url)
        },


        async getUtils_({ commit }) {
            let result = await getUtils()
            sessionStorage.setItem('utils', JSON.stringify(result.data))
            commit('GETUTILS_', result.data)
        }
    },
    getters: {


    }
}